package com.qzdsoft.erpcloud.service.com.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.com.Department;
import com.qzdsoft.erpcloud.domain.sys.User;
import com.qzdsoft.erpcloud.mapper.com.DepartmentMapper;
import com.qzdsoft.erpcloud.mapper.com.DepartmentQueryMapper;
import com.qzdsoft.erpcloud.service.com.DepartmentService;
import com.qzdsoft.erpcloud.vo.com.DepartmentInfo;
import com.qzdsoft.erpcloud.vo.com.DepartmentQueryParam;
import com.qzdsoft.vo.Page;
import com.qzdsoft.vo.PageRequest;
import com.qzdsoft.vo.Response;

@Service
public class DepartmentServiceImpl implements DepartmentService {
	
	@Autowired
	private DepartmentMapper departmentMapper;
	
	@Autowired
	private DepartmentQueryMapper departmentQueryMapper;
	
	
	 @Override
	    public Integer selectDataCount(String name,Integer companyId) {
	        return departmentQueryMapper.selectDataCount(name,companyId);
	    }
	
	
	 @Override
	    public List<Map<String, Object>> selectData(DepartmentQueryParam param) {
		 	PageHelper.startPage(param.getPage(),param.getLimit());
	        return departmentQueryMapper.selectData(param.getName(),param.getCompanyId());
	    }
	
//	@Override
//	public Page<DepartmentInfo> findPage(DepartmentQueryParam params, User user) {
//		PageRequest request = new PageRequest(Integer.parseInt(params.getsEcho()), params.getiDisplayLength());
//        Integer companyId=null;
//        companyId = params.getCompanyId();
//        long count = departmentQueryMapper.countDepartment(params.getSearchKey(),companyId);
//        if (count == 0) {
//            return new Page<DepartmentInfo>(new ArrayList<DepartmentInfo>(), count,params.getsEcho(),request);
//        }
//
//        List<DepartmentInfo> result = departmentQueryMapper.findPageDepartment(request,params.getSearchKey(),companyId);
//        Page<DepartmentInfo> departments = new Page<>(result, count,params.getsEcho(),request);
//        if (departments.getCurrentPage() > departments.getTotalPage()) {
//            request = new PageRequest(departments.getTotalPage(), params.getiDisplayLength());
//            result = departmentQueryMapper.findPageDepartment(request,params.getSearchKey(),companyId);
//            departments = new Page<>( result, count,params.getsEcho(),request);
//        }
//        return departments;
//	}

	@Override
    public Department selectById(Integer departmentId) {
        return departmentMapper.selectByPrimaryKey(departmentId);
    }

	@Override
	@Transactional
	public Response<String> editDepartment(Department department) {
		 	Department dept = departmentMapper.selectByPrimaryKey(department.getId());
		 	dept.setCompanyId(department.getCompanyId());
		 	dept.setName(department.getName());
		 	dept.setRemark(department.getRemark());
	        departmentMapper.updateByPrimaryKey(dept);
	        return Response.success("编辑部门保存成功");
	}

//	 public Response deleteDepartment(Integer departmentId) {
//		  departmentMapper.deleteByPrimaryKey(departmentId);
//	        return Response.success("删除部门成功");
//	    }
	/**
	 * 删除部门
	 */
	 @Override
    public Response del(Integer id) {
        departmentMapper.deleteByPrimaryKey(id);
        return Response.success("删除成功");
    }
	
	public List<Department> getByCompanyId(Integer companyId) {
        return departmentQueryMapper.getByCompanyId(companyId);
    }
	
	 @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }
	 
	 @Override
    public Response edit(Department department) {
        if(!getByName(department.getName(),department.getCompanyId(),department.getId())){
            return Response.error("部门名称重复！");
        }
        if(!getByNo(department.getNo(),department.getCompanyId(),department.getId())){
            return Response.error("部门编号重复！");
        }
        if(null != department.getId()){        //  编辑
//	        	Department dept = departmentMapper.selectByPrimaryKey(department.getId());
//	        	dept.setName(department.getName());
        	department.setCtime(new Date());
            int ui = departmentMapper.updateByPrimaryKey(department);
            return Response.success("编辑成功");
        } else {
            department.setCtime(new Date());
            departmentMapper.insert(department);
            return Response.success("新增成功");
        }
    }
 //判断部门名称是否重复
    public boolean getByName(String name,Integer companyId,Integer id){
        Department department = departmentQueryMapper.getByNameCompanyId(name,companyId);
        if(null==department){
            return true;
        }else{
            if(null==id){
                return false;
            }else{
                if(department.getId()==id){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }
    public boolean getByNo(String no,Integer companyId,Integer id){
        Department department = departmentQueryMapper.getByNoCompanyId(no,companyId);
        if(null==department){
            return true;
        }else{
            if(null==id){
                return false;
            }else{
                if(department.getId()==id){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }
}
